/* ======================== upload.js =============================== */
$("#calc").click(function() {
	callBonus();
});

/* 计算积分 */
function callBonus() {
	var postUrl = 'CallBonus';
	var dataSend = {
		adPosition: $('input[name="adPosition"]:checked').val(),
		adBudget: $('#adBudget').val(),
		adTimes: $('#adTimes').val()
	};
	
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				console.log("资料正确");
				$('#adBonus').val(data.bonus);
				callRank();
			}else{
				console.log("资料错误");
				dialog("您所投放的的金额/次数比，所换算金额不足。<br/>请试着 提高您的预算 或 减少投放次数 。",0);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("伺服器连结错误");
			dialog("伺服器连结错误",2000);
		}
	});
}

/* 计算排名 */
function callRank(){
	var postUrl = 'CallRank';
	var dataSend = {
		adBonus: $('#adBonus').val(),
		adCat: $('input[name="adCat"]:checked').val()
//		adCat1: $('select[id="adCat1"]').val(),
//		adCat2: $('select[id="adCat2"]').val(),
//		adCat3: $('select[id="adCat3"]').val(),
//		adCat4: $('select[id="adCat4"]').val(),
//		adCat5: $('select[id="adCat5"]').val()
	};
	
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				console.log("资料正确");
				$('#adAllRnk').val(data.bonusRank);
				if(data.mainCatRank1 != "")
				$('#adCatRnk1').val(data.mainCatRank1);
				if(data.mainCatRank2 != "")
				$('#adCatRnk2').val(data.mainCatRank2);
				if(data.mainCatRank3 != "")
				$('#adCatRnk3').val(data.mainCatRank3);
				if(data.mainCatRank4 != "")
				$('#adCatRnk4').val(data.mainCatRank4);
				if(data.mainCatRank5 != "")
				$('#adCatRnk5').val(data.mainCatRank5);
			}else{
				console.log("资料错误");
				dialog("资料错误",2000);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			dialog("计算排名异常，请确认",2000);
			console.log("CallRank return fail");
		}
	});
}

function ReCallBonus() {
	var postUrl = 'CallBonus';
	var dataSend = {
		adPosition: $('input[name="adPosition"]:checked').val(),
		adBudget: $('#adBudget').val(),
		adTimes: $('#adTimes').val()
	};
	
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			if(data.status){
				uploadToDB();
			}else{
				console.log("资料错误");
				dialog("您所投放的的金额/次数比，所换算金额不足。<br/>请试着 提高您的预算 或 减少投放次数 。",0);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("伺服器连结错误");
			dialog("伺服器连结错误",2000);
		}
	});
}

function uploadToDB() {
	var postUrl = 'UploadAdv';
	var tempKeyword = $('#adKeyword1').val();
	if($('#adKeyword2').val() !== ''){ tempKeyword += " "+$('#adKeyword2').val(); }
	if($('#adKeyword3').val() !== ''){ tempKeyword += " "+$('#adKeyword3').val(); }
	if($('#adKeyword4').val() !== ''){ tempKeyword += " "+$('#adKeyword4').val(); }
	if($('#adKeyword5').val() !== ''){ tempKeyword += " "+$('#adKeyword5').val(); }
	if($('#adKeyword6').val() !== ''){ tempKeyword += " "+$('#adKeyword6').val(); }
	if($('#adKeyword7').val() !== ''){ tempKeyword += " "+$('#adKeyword7').val(); }
	if($('#adKeyword8').val() !== ''){ tempKeyword += " "+$('#adKeyword8').val(); }
	if($('#adKeyword9').val() !== ''){ tempKeyword += " "+$('#adKeyword9').val(); }
	if($('#adKeyword10').val() !== ''){	tempKeyword += " "+$('#adKeyword10').val(); }
	var dataSend = {
		videoURL: $('#videoURL').val(),
		snapURL: $('#snapURL').val(),
		couponTitle: $('#couponTitle').val(),
		couponURL: $('#couponURL').val(),
		couponAmount: $('#couponAmount').val(),
		userID: $('#userID').val(),
		adTag: $('#adTag').val(),
		adLength: $('#adLength').val(),
		adTitle: $('#adTitle').val(),
		adDescription: $('#adDescription').val(),
		//adKeyword: $('#adKeyword').val(),
		adKeyword: tempKeyword,
		adCat: $('input[name="adCat"]:checked').val(),
		//adCat1: $('#adCat1').val(),
		//adCat2: $('#adCat2').val(),
		//adCat3: $('#adCat3').val(),
		//adCat4: $('#adCat4').val(),
		//adCat5: $('#adCat5').val(),
		//adPosition: $('#adPosition').val(),
		adPosition: $('input[name="adPosition"]:checked').val(),
		adBudget: $('#adBudget').val(),
		adTimes: $('#adTimes').val(),
		adLocation: $('#adLocation').val(),
		adBonus: $('#adBonus').val(),
		adStartTime: $('#adStartTime2').val(),
		adEndTime: $('#adEndTime2').val()
		//adCritic: $('#adCritic').val(),

	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				console.log("资料正确");
				dialog("廣告上傳成功",0);
				setTimeout(window.location.assign(data.redirectPage), 5000);
			}else{
				console.log("资料错误");
				if(data.result == -3){
					dialog("帐户馀额不足",2000);
				}else{
					dialog("资料错误",2000);
				}

			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			dialog("资料错误，请确认",2000);
			console.log("UPLOAD return fail");
		}
	});
}

$("#adBudget").on("keyup focus", function(){
	$('#adBudgetCH').text('人民币' + chineseNumber($('#adBudget').val()) + '元整');
});
$("#adTimes").on("keyup focus", function(){
	$('#adTimesCH').text(chineseNumber($('#adTimes').val()) + '次');
});

function checkValue(){
	if($("#adBonus").val() == ""){
		//alert("积分未计算，请点选按钮");
		//return false;
	}else if($("#adStartTime").val() == ""){
		alert("广告上架日期未选择");
		return false;
	}else if($("#adEndTime").val() == ""){
		alert("广告下架日期未选择");
		return false;
	}
	ReCallBonus();
	return true;
}
